<?
	$items = $form_model->list_related_records_deferred('items', $this->formGetEditSessionKey());
	$this->apply_item_discounts($items);
	
	$has_bundle_items = false;
	foreach ($items as $item)
	{
		if ($item->bundle_master_bundle_item_name)
		{
			$has_bundle_items = true;
			break;
		}
	}
?>

<table class="simpleList">
	<thead>
		<tr>
			<th>Item</th>
			<th>SKU</th>
			<? if ($has_bundle_items): ?>
				<th>Bundle item</th>
			<? endif ?>
			<th class="number">Price</th>
			<th class="number">Discount</th>
			<th class="number">Quantity</th>
			<th class="number <?= !$items->count ? 'last' : null ?>">Total</th>
			<? if ($items->count): ?>
				<th class="iconCell">&nbsp;</th>
				<th class="iconCell last">&nbsp;</th>
			<? endif ?>
		</tr>
	</thead>
	<tbody>
		<? if ($items->count): ?>
			<?
				$session_key = $this->formGetEditSessionKey();
				foreach ($items as $item):
					$onClick = "new PopupForm('onLoadItemForm', {ajaxFields: {'applied_discounts_data': $('applied_discounts_data_field').value, 'item_id': '{$item->id}', 'edit_session_key': '{$session_key}'}}); return false;";
			?>
			<tr class="<?= Backend_Html::zebra('order_items') ?>">
				<td onclick="<?= $onClick ?>" class="link">
					<div class="<?= $item->bundle_master_order_item_id ? 'bundle-item-container' : null ?>">
						<? if ($item->bundle_master_order_item_id): ?>
							<a  href="#" onclick="<?= $onClick ?>" class="tooltip bundle-item" rel="Bundle item - <strong><?= h($item->bundle_master_bundle_item_name) ?></strong>">Bundle item - <?= h($item->bundle_master_bundle_item_name) ?></a>
						<? endif ?>
						
						<a href="#" onclick="<?= $onClick ?>"><?= $item->output_product_name(true, false, false, true) ?></a>
					</div>
				</td>
				<td onclick="<?= $onClick ?>" class="link"><a href="#" onclick="<?= $onClick ?>"><?= $item->product_sku ? h($item->product_sku) : h('<product not found>') ?></a></td>
				<? if ($has_bundle_items): ?>
					<td onclick="<?= $onClick ?>" class="link"><a href="#" onclick="<?= $onClick ?>"><?= h($item->bundle_master_bundle_item_name) ?></a></td>
				<? endif ?>
				<td onclick="<?= $onClick ?>" class="number link"><a href="#" onclick="<?= $onClick ?>"><?= format_currency($item->single_price) ?></a></td>
				<td onclick="<?= $onClick ?>" class="number link"><a href="#" onclick="<?= $onClick ?>"><?= format_currency($item->discount) ?></a></td>
				<td onclick="<?= $onClick ?>" class="number link">
					<a href="#" onclick="<?= $onClick ?>">
						<?= h($item->quantity) ?>
						
						<? if ($item->bundle_master_order_item_id): ?>
							<span rel="Quantity of the bundle<br/>item in each bundle" class="tooltip bundle-item-value"><?= $item->get_bundle_item_quantity() ?></span>
						<? endif ?>
					</a>
				</td>
				<td onclick="<?= $onClick ?>" class="number link"><a href="#" onclick="<?= $onClick ?>"><?= format_currency(($item->single_price - $item->discount)*$item->quantity) ?></a></td>
				<td class="iconCell">
					<? if ($item->product->bundle_items->count): ?>
						<a href="#" class="add-bundle-item" title="Add bundle item" onclick="new PopupForm('onLoadFindBundleProductForm', {ajaxFields: $merge($(this).getForm().fieldsToHash(), {bundle_parent: <?= $item->id ?>})}); return false;">Add bundle item</a>
					<? else: ?>
						<a href="#" class="add-bundle-item disabled" onclick="return false" title="Add bundle item">Add bundle item</a>
					<? endif ?>
				</td>
				<td class="iconCell last">
					<a onclick="return $(this).getForm().sendPhpr('onDeleteItem', {
					extraFields: {item_id: '<?= $item->id ?>'}, 
					confirm: 'Do you really want to delete this item?', 
					update: 'multi', 
					onAfterUpdate: update_tooltips,
					onSuccess: function(){
						shipping_info_changed = true;
						billing_info_changed = true;
					},
					loadIndicator: {
						show: true,
						element: 'item_list',
						hideOnSuccess: true,
						src: 'phproad/resources/images/form_load_30x30.gif'
					}});" title="Delete item" class="deleteDocument" href="#">Delete</a>
				</td>
			</tr>
			<?
				endforeach;
			?>
		<? else: ?>
			<tr class="noData">
				<td colspan="6" class="last">There are no items in this order</td>
			</tr>
		<? endif ?>
	</tbody>
</table>